package ttOrder.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ttOrder.model.po.DinnerTable;
import ttOrder.model.po.Food;
import ttOrder.model.po.OrderDetail;
import ttOrder.model.vo.DinnerTableVO;
import ttOrder.model.vo.FoodVO;
import ttOrder.model.vo.OrderDetailVO;
import ttOrder.model.vo.OrderVO;
import ttOrder.model.vo.UserVO;
import ttOrder.service.DinnerTableServiceImpl;
import ttOrder.service.FoodServiceImpl;
import ttOrder.service.OrderDetailServiceImpl;
import ttOrder.service.OrderServiceImpl;
import ttOrder.service.UserServiceImpl;
import ttOrder.util.CommonMessageResultVO;
import ttOrder.util.CookieUtil;

@Controller
@RequestMapping("/ttOrder")
public class menuController {

	@Resource
	private DinnerTableServiceImpl dinnerTableService;
	@Resource
	private FoodServiceImpl foodService;
	@Resource
	private DinnerTableServiceImpl tableService;
	@Resource
	private OrderServiceImpl orderService;
	@Resource
	private OrderDetailServiceImpl orderDetailService;
	@Resource
	private UserServiceImpl userService;

	@RequestMapping("/index")
	public String getTable(Model model,HttpServletRequest request,HttpServletResponse response) {
		if(CookieUtil.getUserIdFromCookie(request) != null && request.getSession().getAttribute("currentUser") == null){
			UserVO user = new UserVO();
			user.setId(CookieUtil.getUserIdFromCookie(request));
			model.addAttribute("currentUser", userService.getByKey(user));
		}
		
		List<DinnerTableVO> tables = tableService.getAllVO();
		//System.out.println(tables.size());
		model.addAttribute("tables", tables);
		return "Backstage/tables";
	}
	
	@RequestMapping("/menu")
	public String getMenu(Model model,HttpServletRequest request,HttpServletResponse response,int table) {
		List<FoodVO> foods = foodService.getMenu();
		model.addAttribute("foods", foods);
		CookieUtil.saveTableIdToCookie(response,table);
		return "Backstage/menu";
	}
	
	/** <<<< jsp页面跳转  */
	/**  ajax >>>>  */
	
	//添加订单
	@ResponseBody
	@RequestMapping(value="/newOrder",method=RequestMethod.POST)
	public CommonMessageResultVO createOrder(Model model, HttpServletRequest request, /*@RequestParam(value="idArr")*/Integer[] idArr, Integer[] countArr,int totalPrice){
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		Map<String, Object> map = new HashMap<String, Object>();
		if ((idArr!=null)&&(countArr!=null)) {
			OrderVO vo = new OrderVO();
			List<OrderDetailVO> odpl = new ArrayList<OrderDetailVO>();
			int tableId = CookieUtil.getTableIdFromCookie(request);
			OrderVO o = orderService.getVOByTableIdAndStatus(new OrderVO(tableId,0));
			
			if (o == null) {
				//如果该餐桌查不到订单，作如下操作
				for (int i = 0; i < countArr.length; i++) {
					OrderDetailVO odp = new OrderDetailVO();
					odp.setFoodId(idArr[i]);
					odp.setFoodCount(countArr[i]);
					odpl.add(odp);
				}
				vo.setOrderDetailList(odpl);
				vo.setOrderTime(new Date());
				vo.setTableId(tableId);
				vo.setTotalPrice(totalPrice);
				
				try {
					int f = 0;
					f = orderService.insert(vo);
					if (f > 0) {
						vo = orderService.getByKey(new OrderVO(f));
						if (vo != null) {
							map.put("order", vo);
							result.setSuccess(true);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else if (o.getId() > 0) {
				for (int i = 0; i < countArr.length; i++) {
					OrderDetail odp = new OrderDetail();
					odp.setOrderId(o.getId());
					odp.setFoodId(idArr[i]);
					odp.setFoodCount(countArr[i]);
					orderDetailService.insert(odp);
				}
				orderService.updateTotalMoney(o.getId(), totalPrice);
			}
		}else{
			//System.out.println((idArr==null)+" "+(countArr==null));
			map.put("error", "array is null!");
		}
		result.setMessageMap(map);
		return result;
	}
	
	//提交订单，结账
	@ResponseBody
	@RequestMapping(value="subOrder")
	public CommonMessageResultVO getOrder(Integer tableId){
		CommonMessageResultVO vo = new CommonMessageResultVO();
		Map <String,Object>map = new HashMap<String,Object>();
		try {
			OrderVO o = orderService.orderCheckout(tableId);
			if(o!=null){
				vo.setSuccess(true);
				map.put("order", o);
				vo.setMessageMap(map);
			}else{
				vo.setSuccess(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return vo;
	}

}
